import sox
import os
import glob

cwd = os.getcwd()

def sortKeyFunc(s):
    return int(os.path.basename(s)[:-4])

# remix dual mono

# input_filenames = sorted(glob.glob(cwd + '/vocal/*.wav'), key=sortKeyFunc)
# output_directory = cwd + '/remix_vocal'

# def create_output_filename_padded(input_filename, output_dir):
#     return os.path.join(output_dir, 'remix', os.path.split(input_filename)[1])

# for input_file in input_filenames:
#     t = sox.Transformer()
#     remix_dictionary = {1: [1], 2: [1]} 
#     remix_dictionary = {1: [2], 2: [2]}
#     t.remix(remix_dictionary)
#     out_name = create_output_filename_padded(input_file, output_directory)
#     os.makedirs(os.path.split(out_name)[0], exist_ok=True)
#     t.build(input_file, out_name)

# norm - fade - pad

# input_filenames = sorted(glob.glob(cwd + '/remix_field/remix/*.wav'), key=sortKeyFunc)
# output_directory = cwd + '/padded_field'

# def create_output_filename_padded(input_filename, output_dir):
#     return os.path.join(output_dir, 'padded', os.path.split(input_filename)[1])

# for input_file in input_filenames:
#     t = sox.Transformer()
#     t.norm(-3)
#     t.fade(fade_in_len=0.2, fade_out_len=0.2)
#     t.pad(1, 0)
#     out_name = create_output_filename_padded(input_file, output_directory)
#     os.makedirs(os.path.split(out_name)[0], exist_ok=True)
#     t.build(input_file, out_name)



# combine code

cbn = sox.Combiner()
audio_one = sorted(glob.glob(cwd + '/padded_field/padded/*.wav'), key=sortKeyFunc)
audio_two = sorted(glob.glob(cwd + '/padded_vocal/padded/*.wav'), key=sortKeyFunc)
paired_audio = [list(x) for x in zip(audio_one, audio_two)]
# paired_audio = [list(x) for x in zip(audio_two, audio_one)]


def combine():
    for n in paired_audio:
        out_name = os.path.split(n[0])[1].replace('.wav', '') + '_and_' + 'vocal' + os.path.split(n[1])[1]
        cbn.build(n, cwd + '/audio_outputs/' + out_name, 'concatenate')


for afunc in (remix, pad_three_seconds, pad_one_second, combine):
    afunc()
